<?php

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\oms;

use think\Model;
use app\common\common\Constants;
use app\common\model\jsm\Mps;
use app\common\model\jsm\noticeSend;

class MemberReserve extends Model {

    protected $table = 'member_reserve';
    protected $connection = ['database' => 'oms'];

    protected static function init() {
        self::event('before_update', function ($memberReserve) {
            self::_addNoticeSend($memberReserve);
        });
    }

    /**
     * 更新
     * @param type $condition
     * @param type $data
     * @return type
     */
    public function updateByCondition($condition, $data) {
        $save = $this->where()->save($data); //这个必须要用save方法before_insert
        return $save;
    }

    /**
     * 发送白模板
     * @param type $memberReserve
     */
    private function _addNoticeSend($memberReserve) {
        $oldData = self::get($memberReserve->updateWhere);
        $newReserveStatusId = $memberReserve['reserve_status_id'];
        $oldReserveStatusId = $oldData['reserve_status_id'];
        if ('RS_BEFORESHOP' == $oldReserveStatusId && false === empty(Constants::$memberReserveActionArray[$newReserveStatusId])) {
            $mpsModel = new Mps();
            $mpId = $mpsModel->get(['o_id' => $oldData['company_id']])->mp_id;
            $action = Constants::$memberReserveActionArray[$newReserveStatusId];
            $noticeSendModel = new noticeSend();
            $noticeSendModel->save(['member_code' => $oldData['member_code'], 'action' => $action, 'mp_id' => $mpId]);
        }
    }

}
